package com.silence.gankio.widget

import android.app.Activity
import android.content.Context
import android.support.constraint.ConstraintLayout
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import com.silence.gankio.R
import kotlinx.android.synthetic.main.toolbar_comm.view.*

class ToolBarCommView : LinearLayout {
    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)


    init {
        inflate(context, R.layout.toolbar_comm, this)

        iv_title_left.setOnClickListener { onBack(it) }
    }

    private fun onBack(view: View) {
        if (context is Activity) {
            (context as Activity).onBackPressed()
        }
    }

    fun setIvRight(resId: Int, onClickListener: OnClickListener? = null): ToolBarCommView {
        iv_title_right.setImageResource(resId)
        onClickListener?.let {
            iv_title_right.setOnClickListener(it)
        }
        return this
    }

    fun showBack(isShow: Boolean): ToolBarCommView {
        if (!isShow) iv_title_left.visibility = View.GONE else iv_title_left.visibility = View.VISIBLE
        return this
    }

    fun setTitle(title: CharSequence): ToolBarCommView {
        tv_title_center.text = title
        return this
    }

    fun setTitleLeft(title: CharSequence, onClickListener: OnClickListener? = null): ToolBarCommView {
        tv_title_left.text = title
        onClickListener?.let {
            tv_title_left.setOnClickListener(it)
        }
        return this
    }

    fun setTitleRight(title: CharSequence, onClickListener: OnClickListener? = null): ToolBarCommView {
        tv_title_right.text = title
        onClickListener?.let {
            tv_title_right.setOnClickListener(it)
        }
        return this
    }


}